﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Text;

namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class CompanyCheckedList : BasePage
    {
        public CompanyCheckedList()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.企业管理.ToString();
        }

        IBLL_Company ic = Factory.BLL_Company();

        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompanyInfo();
                if (CurrentAccount.GetUser().UserID.Substring(0, 1).Equals("A"))
                {
                    ShowOne.Attributes.Add("style", "display:block;");
                    Show.Attributes.Add("style", "display:none;");
                }
                else
                {
                    ShowOne.Attributes.Add("style", "display:none;");
                    Show.Attributes.Add("style", "display:block;");
                }
                //if (gvwCheckList.Rows.Count <= 0)
                //{
                //    hzbtnAgree.Visible = false;
                //    hzbtnDeny.Visible = false;
                //}
                //if (CurrentAccount.GetUser().UserID == "root")
                //{
                //    this.hzbtnAgreeAll.Enabled = true;
                //}
            }
        }
        /// <summary>
        /// 获取未审核的企业
        /// </summary>
        private void BindCompanyInfo()
        {
            //从页面获取分页大小
            int totalCount = 0 ;
            AspNetPager1.PageSize = 10;
            string strWhere = ""; 
            if (CurrentAccount.GetUser().UserID.Substring(0, 1).Equals("A"))
            {
                if (string.IsNullOrEmpty(DllStatus.SelectedValue) == false)
                {
                    strWhere += " and UnionCityCheckStatus =" + DllStatus.SelectedValue + " ";
                }
                if (string.IsNullOrEmpty(ddlSubmit.SelectedValue) == false)
                {
                    strWhere += " and IsSubmit=" + this.ddlSubmit.SelectedValue + "";
                }
                if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
                {
                    strWhere += " and companyid='" + HZ.Fun.CheckString(txtCode.Text.Trim().ToUpper()) + "'";
                }
                if (!string.IsNullOrEmpty(txtCompanyName.Text.Trim())
                    && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
                {
                    strWhere += " and companyname like '%" + HZ.Fun.CheckString(txtCompanyName.Text.Trim()) + "%' ";
                }
                if (string.IsNullOrEmpty(ddlComType.SelectedValue) == false)
                {
                    strWhere += " and CompanyType=" + ddlComType.SelectedValue + " ";
                }
                
                strWhere += " and areaid like '"+CurrentAccount.GetUser().Org.Area.AreaID.Substring(0,4)+"%'";

                strWhere += "  order by companyname";
                DataTable dt = ic.GetUnionCityCompanyInfoByWhere(strWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwCheckLists.DataSource = dt;
                gvwCheckLists.DataBind();
            }
            else
            {
                if (string.IsNullOrEmpty(DllStatus.SelectedValue) == false)
                {
                    strWhere += " and CheckStatus =" + DllStatus.SelectedValue + " ";
                }
                if (string.IsNullOrEmpty(ddlSubmit.SelectedValue) == false)
                {
                    strWhere += " and TB_CompanyInfo.IsSubmit=" + this.ddlSubmit.SelectedValue + "";
                }
                if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
                {
                    strWhere += " and TB_Company.companyid='" + HZ.Fun.CheckString(txtCode.Text.Trim().ToUpper()) + "'";
                }
                if (!string.IsNullOrEmpty(txtCompanyName.Text.Trim())
                    && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
                {
                    strWhere += " and TB_CompanyInfo.companyname like '%" + HZ.Fun.CheckString(txtCompanyName.Text.Trim()) + "%' ";
                }
                if (string.IsNullOrEmpty(ddlComType.SelectedValue) == false)
                {
                    strWhere += " and CompanyType=" + ddlComType.SelectedValue + " ";
                }
                strWhere += "  order by TB_CompanyInfo.companyname";
                  DataTable dt = ic.GetCompanyInfoByWhere(strWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwCheckList.DataSource = dt;
                gvwCheckList.DataBind();
            }
            

            this.gvwCheckList.Height = this.gvwCheckList.Rows.Count * 30 + 50;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindCompanyInfo();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string getCheckstatus(string str)
        {
            if (str == "1")
            {
                return "<label style='color:green;'>已通过</label>";
            }
            if (str == "2")
            {
                return "<label style='color:red;'>未通过</label>";
            }
            if (str == "0")
            {
                return "未审核";
            }
            return "";
        }

        protected string getSumitstatus(string str)
        {
            if (str == "1")
            {
                return "<label style='color:green;'>已提交</label>";
            }
            else
            {
                return "<label style='color:red;'>未提交</label>";
            }
        }

        protected string UnionCityCheckState(string str)
        {
            if (str == "1")
            {
                return "<label style='color:green;'>已通过</label>";
            }
            if (str == "2")
            {
                return "<label style='color:red;'>未通过</label>";
            }
            return "未审核";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnAgreeAll_Click(object sender, EventArgs e)
        {
            if (iselectRelation.UpdateCompanyInfoCheckStatus("", 0, true) == 1)
            {
                HZ.Web.WebUtil.Alert("操作成功！");

                BindCompanyInfo();
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败！");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnAgree_Click(object sender, EventArgs e)
        {
            updateStatusBatch(1);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void hzbtnDeny_Click(object sender, EventArgs e)
        {
            updateStatusBatch(2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        protected void updateStatusBatch(int status)
        {
            string strCompanyID = string.Empty;
            //遍历列表的所有行
            for (int _index = 0; _index < gvwCheckList.Rows.Count; _index++)
            {
                RadioButton radb = ((RadioButton)gvwCheckList.Rows[_index].FindControl("rdobutton"));
                if (radb.Checked )
                {
                    strCompanyID = this.gvwCheckList.DataKeys[_index].Values["CompanyID"].ToString();
                    break;
                }
            }
            if (string.IsNullOrEmpty(strCompanyID))
            {
                HZ.Web.WebUtil.Alert("请至少选择一条记录！");
                return;
            }
            else
            {
                if (iselectRelation.UpdateCompanyInfoCheckStatus(strCompanyID, status, false) == 1)
                {
                    HZ.Web.WebUtil.Alert("操作成功！");

                    BindCompanyInfo();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("操作失败！");
                }
            }
        }

        protected void gvwCheckList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Add"))
            {
                string comapnyid = e.CommandArgument.ToString();
                Response.Redirect("CompanyCheckInfo.aspx?psid=" + comapnyid + "");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (CurrentAccount.GetUser().UserID.Substring(0, 1).Equals("A"))
            {
                if (string.IsNullOrEmpty(DllStatus.SelectedValue) == false)
                {
                    strWhere += " and UnionCityCheckStatus =" + DllStatus.SelectedValue + " ";
                }
                if (string.IsNullOrEmpty(ddlSubmit.SelectedValue) == false)
                {
                    strWhere += " and IsSubmit=" + this.ddlSubmit.SelectedValue + "";
                }
                if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
                {
                    strWhere += " and companyid='" + HZ.Fun.CheckString(txtCode.Text.Trim().ToUpper()) + "'";
                }
                if (!string.IsNullOrEmpty(txtCompanyName.Text.Trim())
                    && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
                {
                    strWhere += " and companyname like '%" + HZ.Fun.CheckString(txtCompanyName.Text.Trim()) + "%' ";
                }
                if (string.IsNullOrEmpty(ddlComType.SelectedValue) == false)
                {
                    strWhere += " and CompanyType=" + ddlComType.SelectedValue + " ";
                }

                strWhere += " and areaid like '" + CurrentAccount.GetUser().Org.Area.AreaID.Substring(0, 4) + "%'";

                strWhere += "  order by companyname";
                string sql = string.Format(@"SELECT companyid 企业编号,companyname 企业名称,case companytype when 1 then '经营企业' when 2 then '配送企业' when 3 then '生产企业直接配送' when 4 then '经营企业直接配送'
                                             end as 企业类型,checkperson 中心审核人编号,checkdate 中心审核时间,case CheckStatus when 1 then '审核通过' when 2 then '审核不通过' else '未审核' end as 中心审核状态,
                                            CheckReason 中心审核不通过原因,case IsSubmit when 1 then '已提交' else '未提交' end as 企业提交状态 FROM VIEW_AreaCompany WHERE 1=1 {0}", strWhere);
                HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "企业审核信息.xls");
            }
            else
            {
                if (string.IsNullOrEmpty(DllStatus.SelectedValue) == false)
                {
                    strWhere += " and CheckStatus =" + DllStatus.SelectedValue + " ";
                }
                if (string.IsNullOrEmpty(ddlSubmit.SelectedValue) == false)
                {
                    strWhere += " and a.IsSubmit=" + this.ddlSubmit.SelectedValue + "";
                }
                if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
                {
                    strWhere += " and b.companyid='" + HZ.Fun.CheckString(txtCode.Text.Trim().ToUpper()) + "'";
                }
                if (!string.IsNullOrEmpty(txtCompanyName.Text.Trim())
                    && !string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName.Text.Trim())))
                {
                    strWhere += " and a.companyname like '%" + HZ.Fun.CheckString(txtCompanyName.Text.Trim()) + "%' ";
                }
                if (string.IsNullOrEmpty(ddlComType.SelectedValue) == false)
                {
                    strWhere += " and CompanyType=" + ddlComType.SelectedValue + " ";
                }
                strWhere += "  order by a.companyname";
                StringBuilder sql = new StringBuilder();
                sql.Append(string.Format(@"SELECT a.companyid 企业编号,a.companyname 企业名称,case b.companytype when 1 then '经营企业' when 2 then '配送企业' when 3 then '生产企业直接配送' when 4 then '经营企业直接配送'
                                             end as 企业类型,a.checkperson 中心审核人编号,a.checkdate 中心审核时间,case CheckStatus when 1 then '审核通过' when 2 then '审核不通过' else '未审核' end as 中心审核状态,
                                            CheckReason 中心审核不通过原因,case IsSubmit when 1 then '已提交' else '未提交' end as 企业提交状态 
                            FROM TB_CompanyInfo a inner join TB_Company b
            on b.companyid=a.companyid  
            where (companytype<>0 and companytype<>1) {0} ", strWhere));
                HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql.ToString()), "", "企业审核信息.xls");
            }
        }
    }
}
